package com.canbot.u05.utils;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class MyRecorder {

        MediaRecorder recorder;

        private boolean isRecording = false;

        private long startTime;

        private String voiceFilePath = null;

        private String voiceFileName = null;

        private File file;


        /**
         * start recording to the file
         */
        public boolean startRecording(Context appContext) {
                file = null;
//        try {
                if (recorder != null) {
                        recorder.release();
                        recorder = null;
                }
                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setAudioChannels(1); // MONO
                recorder.setAudioSamplingRate(8000); // 8000Hz
                recorder.setAudioEncodingBitRate(64); // seems if change this to
                voiceFilePath = Environment.getExternalStorageDirectory() + "/records/" + "recorder.amr";
                file = new File(voiceFilePath);
                recorder.setOutputFile(file.getAbsolutePath());
                try {
                        recorder.prepare();
                        isRecording = true;
                        recorder.start();
                }
                catch (IllegalStateException e) {
                        isRecording = false;
                        e.printStackTrace();
                        return false;
                }
                catch (IOException e) {
                        isRecording = false;
                        e.printStackTrace();
                        return false;
                }


                Logger.i("voice", file.getAbsolutePath());
//        } catch (IOException e) {
//            EMLog.e("voice", "prepare() failed");
//        }
                startTime = new Date().getTime();
                return true;
//        return file == null ? null : file.getAbsolutePath();
        }

        /**
         * stop the recoding
         *
         * @return seconds of the voice recorded
         */

        public void discardRecording() {
                if (recorder != null) {
                        try {
                                recorder.stop();
                                recorder.release();
                                recorder = null;
                                if (file != null && file.exists() && !file.isDirectory()) {
                                        file.delete();
                                }
                        }
                        catch (IllegalStateException e) {
                        }
                        catch (RuntimeException e) {
                        }
                        isRecording = false;
                }
        }

        public int stopRecoding() {
                if (recorder != null) {
                        isRecording = false;


                        try {
                                recorder.stop();
                        }
                        catch (IllegalStateException e) {
                                e.printStackTrace();
                        }
                        recorder.release();
                        recorder = null;

                        if (file == null || !file.exists() || !file.isFile()) {
                                return -1;
                        }
                        if (file.length() == 0) {
                                file.delete();
                                return -1;
                        }
                        int seconds = (int) (new Date().getTime() - startTime) / 1000;
                        Logger.i("voice", "stopRecoding----->seconds:" + seconds + " file length:" + file.length());
                        return seconds;
                }
                return 0;
        }

        protected void finalize() throws Throwable {
                super.finalize();
                if (recorder != null) {
                        recorder.release();
                }
        }


//    public boolean isRecording() {
//        return isRecording;
//    }


        public String getVoiceFilePath() {
                return voiceFilePath;
        }

        public String getVoiceFileName() {
                return voiceFileName;
        }
}
